<template>
  <div>
    <Tabs>
      <TabPane label="新增配置" icon="logo-apple">
        工作目录
        <input type="file" @change="triggerFile($event)" webkitdirectory/>
        <template v-if="showTree">
          <Button type="info" @click="setGit">监听</Button>
          <Table
            :border="true"
            :stripe="true"
            ref="selection"
            :columns="gitListColoumns"
            :data="gitList"
            :tooltip="true"
            @on-select="tableSelect"
            @on-select-cancel="tableSelectCancel"
            @on-select-all="tableSelectAll(true)"
            @on-select-all-cancel="tableSelectAll(false)"
          ></Table>
        </template>
      </TabPane>
      <TabPane label="当前列表" icon="logo-windows">当前列表</TabPane>
      <TabPane label="操作日志" icon="logo-tux">操作日志</TabPane>
      
    </Tabs>
  </div>
</template>
 
<script>
import git from "../../lib/git";
export default {
  data() {
    return {
      filePath: "",
      gitListColoumns: [
        {
          title: "#",
          key: "index",
          width: 60,
          align: "center",
        },
        {
          title: "分支名",
          key: "name",
          align: "center",
        },
        {
          type: "selection",
          width: 60,
          align: "center",
        },
      ],
      gitList: [{}],
      showTree: false, //是否展示git的代码树
    };
  },
  methods: {
    triggerFile(event) {
      let filePath = event.target.files[0].path;
      console.log(filePath);
      let response = git.checkGit(filePath);
      this.filePath = filePath;

      if (response != "true") {
        if (response == "false") {
          this.$Message.error("不是git仓库，请重新选择!");
          this.showTree = false;
        } else {
          this.$Message.error(response);
        }
      } else {
        this.showTree = true;
        let res = git.getLocalBranch(filePath);
        if (res.code != true) {
          this.$Message.error(res.data);
        } else {
          let list = git.parseGitBranch(res.data, filePath);
          this.gitList = list;
        }
      }
    },
    tableSelect(selection, row) {
      this.gitList[row.index - 1]._checked = true;
    },
    //取消选择
    tableSelectCancel(selection, row) {
      this.gitList[row.index - 1]._checked = false;
    },

    //点击每一列的右边按钮触发的操作
    tableSelectAll(status) {
      for (let i = 0; i < this.gitList.length; i++) {
        if (this.gitList[i]) {
          this.gitList[i]._checked = status;
        }
      }
    },
    //递交处理器
    setGit() {
      let todoList = [];
      for (let i = 0; i < this.gitList.length; i++) {
        if (this.gitList[i]._checked) {
          todoList.push({
            branch: this.gitList[i].name,
            path: this.gitList[i].filePath,
          });
        }
      }
      if(todoList.length ===0 ){
          this.$Message.warning("请选择需要监听的分支");
          return;
      }
      let res = git.setTodoList(todoList);
      if (res.code == 0) {
        this.$Message.success(res.data);
      } else if (res.code == 1) {
        this.$Message.warning(res.data);
      } else {
        this.$Message.error(res.data);
      }
    },
    // handleSelectAll(status) {
    //   this.$refs.selection.selectAll(status);
    //   for (let i = 0; i < this.rawFileList.length; i++) {
    //     this.rawFileList[i]._checked = status;
    //   }

    // },
  },
};
</script>

<style>
</style>